package handler

import (
	"bike-vb/cache"
	"bike-vb/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

// 仓库添加
func RepoAdd(c *gin.Context) {
	rep_code := c.PostForm("rep_code")
	repo_name := c.PostForm("repo_name")
	if rep_code == "" || repo_name == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.Repo{
		RepCode:  rep_code,
		RepoName: repo_name,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

// 订单添加
func RepoOrderAdd(c *gin.Context) {
	rep_id, _ := strconv.Atoi(c.PostForm("rep_id"))
	order_code := c.PostForm("order_code")
	good_num, _ := strconv.Atoi(c.PostForm("good_num"))
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	if rep_id == 0 || order_code == "" || good_num == 0 || order_price == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.RepoOrder{
		RepId:      rep_id,
		OrderCode:  order_code,
		GoodNum:    good_num,
		OrderPrice: order_price,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

// 订单列表
func RepoOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size

	var list []model.RepoOrderList
	/*
		SELECT
		  repo_orders.id AS OrderID,
		  repos.rep_code AS RepCo,
		  repos.repo_name AS RepoName,
		  repo_orders.order_code AS OrderCode,
		  repo_orders.good_num AS GoodNum,
		  repo_orders.order_price AS OrderPrice
		FROM
		  repo_orders
		  LEFT JOIN repos ON repos.id = repo_orders.rep_id
	*/

	redis_key := "repo_order_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("repo_orders").
			Select("repo_orders.id AS OrderID",
				" repos.rep_code AS RepCode",
				"repos.repo_name AS RepoName",
				"repo_orders.order_code AS OrderCode",
				"repo_orders.good_num AS GoodNum",
				"repo_orders.order_price AS OrderPrice",
			).
			Joins("LEFT JOIN repos ON repos.id = repo_orders.rep_id").
			Offset(offset).Limit(size).Debug().Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": list,
	})
	return
}

// 订单详情
func RepoOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var repoOrder model.RepoOrderList
	redis_key := "repo_order_list_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	/*
		SELECT
		  repo_orders.id AS OrderID,
		  repos.rep_code AS RepCo,
		  repos.repo_name AS RepoName,
		  repo_orders.order_code AS OrderCode,
		  repo_orders.good_num AS GoodNum,
		  repo_orders.order_price AS OrderPrice
		FROM
		  repo_orders
		  LEFT JOIN repos ON repos.id = repo_orders.rep_id
	*/
	if cacheData == "" {
		model.DB.Table("repo_orders").
			Select("repo_orders.id AS OrderID",
				" repos.rep_code AS RepCode",
				"repos.repo_name AS RepoName",
				"repo_orders.order_code AS OrderCode",
				"repo_orders.good_num AS GoodNum",
				"repo_orders.order_price AS OrderPrice",
			).
			Joins("LEFT JOIN repos ON repos.id = repo_orders.rep_id").
			Where("repo_orders.id = ?", id).
			Debug().
			Find(&repoOrder)
		jsonData, _ := json.Marshal(repoOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, string(jsonData), time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &repoOrder)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": repoOrder,
	})
	return
}
